package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.AuthCode;
import com.kch.system.service.AuthCodeService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/auth")
@Api(tags = "认证码管理")
public class AuthCodeController extends BaseController {
    private String prefix = "system/auth";

    @Autowired
    private AuthCodeService authCodeService;

    @RequiresPermissions("system:auth:view")
    @GetMapping()
    public String auth(){
        return prefix + "/auth";
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated AuthCode authCode) {
        authCode.setCreateBy(ShiroUtils.getLoginName());
        authCode.setCreateTime(new Date());
        authCodeService.insertAuth(authCode);
        return toAjax(true);
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        startPage();
        List<AuthCode> list = authCodeService.selectAuthList();
        return getDataTable(list);
    }

    @Log(title = "认证码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(authCodeService.deleteAuth(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
